define([
    'text!./notice_table_widget.html',
    '../../service',
    '../../utils/i18n',
    "pager"
], function(sourceHTML, api, i18n) {
    var widget = avalon.ui.noticeTableWidget = function(element, data, vmodels) {
        var vmodel = avalon.define(data.noticeTableWidgetId, function(vm) {
            vm.results = {};
            vm.collections = [];
            vm.navSearchList = [];
            vm.params = {
                method: null,
                query: {

                }
            }
            vm.NoticeType = 0;
            vm.pager = {
                currentPage: vm.currentPage || 1,
                perPages: vm.perPages || 8,
                showJumper: false,
                alwaysShowNext: true, //@config {Boolean} 总是显示向后按钮
                alwaysShowPrev: true,
                pageCount: 1

            }; // 36/4=9
            vm.pager.onJump = function(e, data) {
                // if backend handle pages
                //location.href = "#page=" + data.currentPage
                vm.params.query.PageIndex = data.currentPage;
                vm._getAndRenderCollections(vm.params, function(res) {
                    vm.pager.currentPage = data.currentPage;
                });

            }
            _.merge(vm, data.noticeTableWidgetOptions)
            vm.$skipArray = [
                "results", "pager", "params", "collections", "categories", "navSearchList"
            ];
            vm.returnCurrent = function(key) {
                return vm[key]
            }
            vm.changeCurrent = function(evt, el, c, elem) {
                vm[el] = c.guid
                vm.params.query[el] = c.guid;
                if (c.guid == 0)
                    delete vm.params.query[el];
                vm.$fire("searching", vm.params)
            }
            vm.getNoticeType = function() {
                return api.getItems({
                    method: api.getNoticesType
                }, function(res) {
                    if (res && res[api.collectionsName]) {
                        var tmpSearch = {};
                        tmpSearch.collections = res[api.collectionsName];
                        tmpSearch.name = "NoticeType";
                        tmpSearch.title = i18n.gettext("NoticeType")
                        vm.navSearchList.push(tmpSearch)
                    }

                })

            }
            vm._getAndRenderCollections = function(query, cb) {

                return api.getItems(avalon.mix(vmodel.params), function(res) {

                    if (res && res[api.collectionsName])
                        vm.results = res;

                    vm.collections = vm.results[api.collectionsName];

                    // //stub
                    if (vm.collections.length > vm.perPages) {
                        vm.collections = vm.collections.slice(0, vm.perPages);
                    }

                    _.each(vm.collections, function(el) {
                        var n = el.CommentCount || 0;
                        el.collectionsCount = i18n.ngettext("collections %s person", "collections %s people", n);

                    })

                    vm.pager.totalItems = vm.results.query.recordCount;
                    vm.pager.currentPage = vm.results.query.pageIndex;
                    vm.pager.perPages = vm.results.query.pageSize;
                    vm.pager.pageCount = vm.results.query.pageCount;
 
                    element.innerHTML = sourceHTML;
                    avalon.scan(element, [vmodel].concat(vmodels));

                    if (cb) {
                        cb(res);
                    }

                })

            }


            vm.$init = function() {

                new Promise(function(res, rej) {
                    res()
                }).then(function() {
                    return vm.getNoticeType()
                }).then(function() {
                    vm._getAndRenderCollections()
                })
            }

            vm.$remove = function() {
                element.innerHtml = element.textContent = "";
            }
        });
        vmodel.$watch("searching", function(query) {
            vmodel._getAndRenderCollections(query)
        })
        return vmodel;
    }
    return avalon;

});
